using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace Utilities.Menu.UIElements
{
    public class VirtualKeyboard
    {
        static readonly int OFFSET = 50;
        static readonly int PADDING = 10;
        static readonly int ROWS_SPACING = 60;

        Button[] keyboard_buttons;
        SpriteFont font;
        Texture2D btn_bg, del_bg, key_bg;
        SoundEffect click;
        TextEdit text_edit;

        public VirtualKeyboard(TextEdit text_edit, SpriteFont font, SoundEffect click, Texture2D keyboard_bg, Texture2D btn_background, Texture2D delete_button, int starting_x, int starting_y)
        {
            int j = 0;
            char curr_lett = 'A';

            this.font = font;
            this.click = click;
            this.text_edit = text_edit;
            keyboard_buttons = new Button[27];

            Vector2 lett_measure = font.MeasureString(curr_lett.ToString());

            for (int i = 0; i < 26; i++)
            {
                if (i % 9 == 0)
                    j++;

                int x = starting_x + OFFSET * (i % 9), y = starting_y + ROWS_SPACING * j;
                keyboard_buttons[i] = new Button { CollisionBox = new Rectangle(x - PADDING, y - PADDING, (PADDING * 2 + (int)lett_measure.X), (PADDING * 2 + (int)lett_measure.Y)), Label = curr_lett.ToString(), Position = new Vector2(x, y) };

                curr_lett++;
            }

            int xd = starting_x + OFFSET * (26 % 9), yd = starting_y + ROWS_SPACING * 3;
            keyboard_buttons[26] = new Button { CollisionBox = new Rectangle(xd - PADDING, yd - PADDING, (PADDING * 2 + (int)lett_measure.X), (PADDING * 2 + (int)lett_measure.Y)), Label = " ", Position = new Vector2(xd, yd) };

            btn_bg = btn_background;
            del_bg = delete_button;
            key_bg = keyboard_bg;
        }

        public VirtualKeyboard(TextEdit text_edit, SpriteFont font, SoundEffect click, Texture2D keyboard_bg, Texture2D btn_background, Texture2D delete_button, int starting_x, int starting_y, bool qwerty)
        {
            int j = 0;
            char[] querty_letters = {'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P',
                                     'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L',
                                     'Z', 'X', 'C', 'V', 'B', 'N', 'M'};

            this.font = font;
            this.click = click;
            this.text_edit = text_edit;
            keyboard_buttons = new Button[27];
            starting_x -= 30;

            Vector2 lett_measure = font.MeasureString(querty_letters[0].ToString());

            for (int i = 0; i < 26; i++)
            {
                if (i == 10 || i == 19)
                    j++;

                int x, y;

                if (i <= 9)
                {
                    x = starting_x + OFFSET * i;
                    y = starting_y + ROWS_SPACING * j;
                }
                else if (i <= 18)
                {
                    x = starting_x + OFFSET * (i % 10) + 20;
                    y = starting_y + ROWS_SPACING * j;
                }
                else
                {
                    x = starting_x + OFFSET * (i % 19) + 40;
                    y = starting_y + ROWS_SPACING * j;
                }

                keyboard_buttons[i] = new Button { CollisionBox = new Rectangle(x - PADDING, y - PADDING, (PADDING * 2 + (int)lett_measure.X), (PADDING * 2 + (int)lett_measure.Y)), Label = querty_letters[i].ToString(), Position = new Vector2(x, y) };
            }

            int xd = starting_x + OFFSET * (26 % 19) + 40, yd = starting_y + ROWS_SPACING * 2;
            keyboard_buttons[26] = new Button { CollisionBox = new Rectangle(xd - PADDING, yd - PADDING, (PADDING * 2 + (int)lett_measure.X), (PADDING * 2 + (int)lett_measure.Y)), Label = " ", Position = new Vector2(xd, yd) };

            btn_bg = btn_background;
            del_bg = delete_button;
            key_bg = keyboard_bg;
        }

        public void DrawKeyboard(GameTime gameTime, SpriteBatch sprite_batch)
        {
            sprite_batch.Draw(key_bg, new Vector2(sprite_batch.GraphicsDevice.Viewport.Width / 2 - key_bg.Width / 2, keyboard_buttons[0].Position.Y - 30), Color.Gray);

            for (int i = 0; i < keyboard_buttons.Count() - 1; i++)
			{
                sprite_batch.Draw(btn_bg, keyboard_buttons[i].CollisionBox, Color.White);
                sprite_batch.DrawString(font, keyboard_buttons[i].Label, keyboard_buttons[i].Position, Color.White);
			}

            text_edit.DrawTextEdit(gameTime, sprite_batch);

            sprite_batch.Draw(del_bg, keyboard_buttons[26].CollisionBox, Color.White);
        }

        public void DrawKeyboard(GameTime gameTime, SpriteBatch sprite_batch, float alpha)
        {
            sprite_batch.Draw(key_bg, new Vector2(sprite_batch.GraphicsDevice.Viewport.Width / 2 - key_bg.Width / 2, keyboard_buttons[0].Position.Y - 30), Color.Gray * alpha);

            for (int i = 0; i < keyboard_buttons.Count() - 1; i++)
            {
                sprite_batch.Draw(btn_bg, keyboard_buttons[i].CollisionBox, Color.White * alpha);
                sprite_batch.DrawString(font, keyboard_buttons[i].Label, keyboard_buttons[i].Position, Color.White * alpha);
            }

            text_edit.DrawTextEdit(gameTime, sprite_batch, alpha);

            sprite_batch.Draw(del_bg, keyboard_buttons[26].CollisionBox, Color.White * alpha);
        }

        public void Update(Vector2 t_position)
        {
            for (int i = 0; i < keyboard_buttons.Count(); i++)
            {
                if (keyboard_buttons[i].CollisionBox.Contains((int)t_position.X, (int)t_position.Y))
                {
                  var volume = 1.0f;
                  if (MediaPlayer.State == MediaState.Playing) volume = 0;
                  click.Play(volume, 0.0f, 0.0f);
                    if (keyboard_buttons[i].Label != " ")
                        text_edit.AddChar(keyboard_buttons[i].Label);
                    else
                        text_edit.RemoveChar();
                }
            }
        }
    }
}
